home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks95 / NewtANoteSuckANote.sit / Newt-A-Note & Suck-A-Note 1.0 / Newt-A-Note / DSUserProcs.c < prev    next >
Text File  |  1995-06-24  |  13KB  |  428 lines

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUserProcs.c
  5. **
  6. **   Description:    Specific AppleEvent handlers used by the DropBox
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Time    Author    Description
  23. **    --------    -----    ------    ---------------------------------------------
  24. **    06/23/94            LDR        Added support for ProcessItem and ProcessFolder handling
  25. **    02/20/94            LDR        Modified Preflight & Postflight to take item count
  26. **    01/25/92            LDR        Removed the use of const on the userDataHandle
  27. **    12/09/91            LDR        Added the new SelectFile userProc
  28. **                                Added the new Install & DisposeUserGlobals procs
  29. **                                Modified PostFlight to only autoquit on odoc, not pdoc
  30. **    11/24/91            LDR        Added the userProcs for pdoc handler
  31. **                                Cleaned up the placement of braces
  32. **                                Added the passing of a userDataHandle
  33. **    10/29/91            SCS        Changes for THINK C 5
  34. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  35. **                                Added a bunch of comments for clarification
  36. **    10/06/91    00:02    MTC        Converted to MPW C
  37. **    04/09/91    00:02    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #include <StandardFile.h>
  42.  
  43. #include "DSGlobals.h"
  44. #include "DSUserProcs.h"
  45. #include "LlamaTalk.h"
  46. #include "Comms.h"
  47.  
  48. // Static Prototypes
  49. static OSErr ProcessItem(FSSpecPtr myFSSPtr);
  50. static OSErr ProcessFolder(FSSpecPtr myFSSPtr);
  51.  
  52.  
  53. /*
  54.     Uncomment this line if you want each item of a dropped folder processed
  55.     as an individual item
  56. */
  57. #define qWalkFolders
  58.  
  59.  
  60. /*
  61.     This routine is called during init time.
  62.     
  63.     It allows you to install more AEVT Handlers beyond the standard four
  64. */
  65. #pragma segment Main
  66. pascal void InstallOtherEvents (void) {
  67. }
  68.  
  69.  
  70. /*    
  71.     This routine is called when an OAPP event is received.
  72.     
  73.     Currently, all it does is set the gOApped flag, so you know that
  74.     you were called initally with no docs, and therefore you shouldn't 
  75.     quit when done processing any following odocs.
  76. */
  77. #pragma segment Main
  78. pascal void OpenApp (void) {
  79.     gOApped = true;
  80. }
  81.  
  82.  
  83. /*    
  84.     This routine is called when an QUIT event is received.
  85.     
  86.     We simply set the global done flag so that the main event loop can
  87.     gracefully exit.  We DO NOT call ExitToShell for two reasons:
  88.     1) It is a pretty ugly thing to do, but more importantly
  89.     2) The Apple event manager will get REAL upset!
  90. */
  91. #pragma segment Main
  92. pascal void QuitApp (void) {
  93.     gDone = true;    /*    All Done! */
  94. }
  95.  
  96.  
  97. /*    
  98.     This routine is the first one called when an ODOC or PDOC event is received.
  99.     
  100.     In this routine you would place code used to setup structures, etc. 
  101.     which would be used in a 'for all docs' situation (like "Archive all
  102.     dropped files")
  103.  
  104.     Obviously, the opening boolean tells you whether you should be opening
  105.     or printing these files based on the type of event recieved.
  106.     
  107.     NEW IN 2.0!
  108.     The itemCount parameter is simply the number of items that were dropped on
  109.     the application and that you will be processing.  This gives you the ability
  110.     to do a single preflight for memory allocation needs, rather than doing it
  111.     once for each item as in previous versions.
  112.     
  113.     userDataHandle is a handle that you can create & use to store your own
  114.     data structs.  This dataHandle will be passed around to the other 
  115.     odoc/pdoc routines so that you can get at your data without using
  116.     globals - just like the new StandardFile.  
  117.     
  118.     We also return a boolean to tell the caller if you support this type
  119.     of event.  By default, our dropboxes don't support the pdoc, so when
  120.     opening is FALSE, we return FALSE to let the caller send back the
  121.     proper error code to the AEManager.
  122.  
  123.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  124. */
  125. #pragma segment Main
  126. pascal Boolean PreFlightDocs (Boolean opening, short itemCount, Handle *userDataHandle) {
  127. #pragma unused ( itemCount )
  128. #pragma unused ( userDataHandle )
  129.  
  130.     return opening;        // we support opening, but not printing - see above
  131. }
  132.  
  133.  
  134. /*    
  135.     This routine is called for each file passed in the ODOC event.
  136.     
  137.     In this routine you would place code for processing each file/folder/disk that
  138.     was dropped on top of you.
  139.     
  140.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  141. */
  142. #pragma segment Main
  143. pascal void OpenDoc ( FSSpecPtr myFSSPtr, Boolean opening, Handle userDataHandle ) {
  144. #pragma unused ( myFSSPtr )
  145. #pragma unused ( opening )
  146. #pragma unused ( userDataHandle )
  147.     OSErr    err = noErr;
  148.     
  149.     
  150.     #ifdef qWalkFolders
  151.     /*
  152.         For this case we need to determine if the FSSpec is a file or folder.
  153.         If it's a folder, we then need to process each item in that folder,
  154.         otherwise just process the item.
  155.     */
  156.     if (FSpIsFolder(myFSSPtr))
  157.         err = ProcessFolder(myFSSPtr);
  158.     else
  159.         err = ProcessItem(myFSSPtr);
  160.     #else
  161.     /*
  162.         For this case we just call ProcessItem on the FSSpec above.
  163.     */
  164.     err = ProcessItem(myFSSPtr);
  165.     #endif
  166.     
  167.     // you should probably do something if you get back an error ;)
  168. }
  169.  
  170.  
  171. /*    
  172.     This routine is the last routine called as part of an ODOC event.
  173.     
  174.     In this routine you would place code to process any structures, etc. 
  175.     that you setup in the PreflightDocs routine.
  176.  
  177.     NEW IN 2.0!
  178.     The itemCount parameter was the number of items that you processed.
  179.     It is passed here just in case you need it ;)  
  180.     
  181.     If you created a userDataHandle in the PreFlightDocs routines, this is
  182.     the place to dispose of it since the Shell will NOT do it for you!
  183.     
  184.     You will probably want to remove the #pragma unusued (currently there to fool the compiler!)
  185. */
  186. #pragma segment Main
  187. pascal void PostFlightDocs ( Boolean opening, short itemCount, Handle userDataHandle ) {
  188. #pragma unused ( opening )
  189. #pragma unused ( itemCount )
  190. #pragma unused ( userDataHandle )
  191.  
  192.     short i;
  193.     char * s = "ByeBye!";
  194.     
  195.     if ( (opening) && (!gOApped) )    {
  196.         gOutDataHdl = NewHandleClear(7);
  197.         if (gOutDataHdl) {
  198.             BlockMove(s, *gOutDataHdl, 7);
  199.             LTWrite(gLlamaTalkGlobals, gSocket, &gOutDataHdl);
  200.             for (i=0;i<32;i++)
  201.                 LTIdle(gLlamaTalkGlobals);
  202.         }
  203.  
  204.  
  205.  
  206.         gDone = true;    //    close everything up!
  207.     }
  208.  
  209.     /*
  210.         The reason we do not auto quit is based on a recommendation in the
  211.         Apple event Registry which specifically states that you should NOT
  212.         quit on a 'pdoc' as the Finder will send you a 'quit' when it is 
  213.         ready for you to do so.
  214.     */
  215. }
  216.  
  217.  
  218. /*
  219.     This routine gets called for each item (which could be either a file or a folder)
  220.     that the caller wants dropped.  The determining factor is the definition of the 
  221.     qWalkFolder compiler directive.   Either way, the item in question should be
  222.     processed as a single item and not "dissected" into component units (like subfiles
  223.     of a folder!)
  224. */
  225. static OSErr ProcessItem(FSSpecPtr myFSSPtr)
  226. {
  227.     OSErr        err = noErr;
  228.     short        ref = 0;
  229.     long        len;
  230.     CInfoPBRec    pb;        // 108-byte area
  231.     
  232.     err = FSpOpenDF(myFSSPtr, fsRdPerm, &ref);
  233.     if (err == noErr) {
  234.         err = SetFPos(ref, 1, 0);
  235.         
  236.         pb.hFileInfo.ioCompletion = FALSE;
  237.         pb.hFileInfo.ioFDirIndex = -1;
  238.         pb.hFileInfo.ioVRefNum = myFSSPtr->vRefNum;
  239.         pb.hFileInfo.ioDirID = myFSSPtr->parID;
  240.         pb.hFileInfo.ioNamePtr = myFSSPtr->name;
  241.         err = PBGetCatInfo(&pb, FALSE);
  242.         len = pb.hFileInfo.ioFlLgLen;
  243.         
  244.         gOutDataHdl = NewHandleClear(len + 1);
  245.         if (gOutDataHdl) {
  246.             err = FSRead(ref, &len, *gOutDataHdl);
  247.             if (err == noErr) {
  248.                 LTWrite(gLlamaTalkGlobals, gSocket, &gOutDataHdl);
  249.             }
  250.         }
  251.         
  252.         FSClose(ref);
  253.     }
  254.     
  255.     return(err);
  256. }
  257.  
  258. /*
  259.     This routine gets called for any folder (or disk) that the caller wants 
  260.     processed as a set of component items, instead of as a single entity.
  261.     The determining factor is the definition of the qWalkFolder compiler directive.
  262. */
  263. static OSErr ProcessFolder(FSSpecPtr myFSSPtr)
  264. {
  265.     OSErr        err = noErr;
  266.     short        index, oldIndex, localIndex;
  267.     FSSpec        localFSSpec, curFSSpec;
  268.     CInfoPBRec    cipb;
  269.     Str255        fName, vFName;
  270.     long        dirID, origDirID;
  271.     Boolean        foundPosition;
  272.  
  273.      // copy the source locally to avoid recursion problems
  274.      BlockMoveData(myFSSPtr, &localFSSpec, sizeof(FSSpec));
  275.      
  276.     //    get the dirID for THIS folder, not it's parent!
  277.     BlockMoveData(localFSSpec.name, fName, 32);
  278.     
  279.     cipb.hFileInfo.ioCompletion    = 0L;
  280.     cipb.hFileInfo.ioNamePtr    = fName;
  281.     cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  282.     cipb.hFileInfo.ioFDirIndex    = 0;    // use the dir & vRefNum;
  283.     cipb.hFileInfo.ioDirID        = localFSSpec.parID;
  284.     err = PBGetCatInfoSync(&cipb);
  285.     
  286.     if (!err) {        
  287.         origDirID = cipb.dirInfo.ioDrDirID; // copy the sucker
  288.         index = 1;
  289.                 
  290.         // index through all contents of this folder
  291.         while (err == noErr) {
  292.             dirID = origDirID;
  293.             localIndex = index;
  294.             fName [0] = 0;
  295.             cipb.hFileInfo.ioCompletion    = 0L;
  296.             cipb.hFileInfo.ioNamePtr    = fName;
  297.             cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  298.             cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  299.             cipb.hFileInfo.ioDirID        = dirID;
  300.             err = PBGetCatInfoSync(&cipb);
  301.  
  302.             if (!err) {
  303.                 BlockMoveData(fName, curFSSpec.name, 32);
  304.                 curFSSpec.vRefNum    = cipb.hFileInfo.ioVRefNum;
  305.                 curFSSpec.parID        = dirID;
  306.             
  307.                 /*    
  308.                     Check to see if this entry is a folder.
  309.                 */
  310.                 if (cipb.hFileInfo.ioFlAttrib & ioDirMask) {
  311.                     err = ProcessFolder(&curFSSpec);
  312.                  } else
  313.                     err = ProcessItem(&curFSSpec);
  314.             
  315.                 /*    If we've had an error, get out! */
  316.                 if (err)    break;
  317.  
  318.                 // dirID = origDirID;    
  319.                 localIndex = index;    
  320.  
  321.                 /*    
  322.                     Now take into account new files being created
  323.                     in the current directory & messing up our index.
  324.                     See Dev.CD Vol. XI:Tools & Apps (Moof!):Misc Utilities:
  325.                     Disinfectant & Source 2.5.1:Sample:Notes:Scan Alg    
  326.                 */
  327.                 vFName [0] = 0;
  328.                 cipb.hFileInfo.ioCompletion    = 0L;
  329.                 cipb.hFileInfo.ioNamePtr    = vFName;
  330.                 cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  331.                 cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  332.                 cipb.hFileInfo.ioDirID        = dirID;
  333.                 err = PBGetCatInfoSync(&cipb);
  334.                 oldIndex = index;
  335.                 if (!err) {
  336.                     /*    If they're equal - same place, go to next */
  337.                     if (EqualString (vFName, fName, false, false))
  338.                         index++;
  339.                 }
  340.                 
  341.                 /*    If we didn't advance, then perhaps a file was created or deleted */
  342.                 if (oldIndex == index) {
  343.                     oldIndex        = index;    /* save off the old */
  344.                     index            = 0;        /* and start at the beginning */
  345.                     err                = noErr;
  346.                     vFName [0]        = 0;
  347.                     foundPosition    = false;
  348.                     
  349.                     while (!foundPosition) {
  350.                         index++;
  351.                         vFName [0] = 0;
  352.                         cipb.hFileInfo.ioCompletion    = 0L;
  353.                         cipb.hFileInfo.ioNamePtr    = vFName;
  354.                         cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  355.                         cipb.hFileInfo.ioFDirIndex    = index;    /* now use a real index */
  356.                         cipb.hFileInfo.ioDirID        = dirID;
  357.                         err = PBGetCatInfoSync(&cipb);
  358.                         
  359.                         if (err == fnfErr) {  // we've just been deleted
  360.                             index = oldIndex;
  361.                             foundPosition = true;
  362.                             err = noErr;    // have to remember to reset this!
  363.                         }
  364.                         
  365.                     /*    found same file & same index position */
  366.                     /*    so try the next item */
  367.                         if ((!foundPosition) && EqualString(fName, vFName, false, false)) {
  368.                             index++;
  369.                             foundPosition = true;
  370.                         }
  371.                     }
  372.                 }
  373.             }
  374.         }
  375.     }
  376.     
  377.     return(err);
  378. }
  379.  
  380. /*
  381.     This routine is called when the user chooses "Select File…" from the
  382.     File Menu.
  383.     
  384.     Currently it simply calls the new StandardGetFile routine to have the
  385.     user select a single file (any type, numTypes = -1) and then calls the
  386.     SendODOCToSelf routine in order to process it.  
  387.             
  388.     The reason we send an odoc to ourselves is two fold: 1) it keeps the code
  389.     cleaner as all file openings go through the same process, and 2) if events
  390.     are ever recordable, the right things happen (this is called Factoring!)
  391.  
  392.     Modification of this routine to only select certain types of files, selection
  393.     of multiple files, and/or handling of folder & disk selection is left 
  394.     as an exercise to the reader.
  395. */
  396. pascal void SelectFile (void)
  397. {
  398.     StandardFileReply    stdReply;
  399.     SFTypeList            theTypeList;
  400.  
  401.     StandardGetFile(NULL, -1, theTypeList, &stdReply);
  402.     if (stdReply.sfGood)    // user did not cancel
  403.         SendODOCToSelf(&stdReply.sfFile);    // so send me an event!
  404. }
  405.  
  406. /*
  407.     This routine is called during the program's initialization and gives you
  408.     a chance to allocate or initialize any of your own globals that your
  409.     dropbox needs.
  410.     
  411.     You return a boolean value which determines if you were successful.
  412.     Returning false will cause DropShell to exit immediately.
  413. */
  414. pascal Boolean InitUserGlobals(void)
  415. {
  416.     return(true);    // nothing to do, it we must be successful!
  417. }
  418.  
  419. /*
  420.     This routine is called during the program's cleanup and gives you
  421.     a chance to deallocate any of your own globals that you allocated 
  422.     in the above routine.
  423. */
  424. pascal void DisposeUserGlobals(void)
  425. {
  426.     // nothing to do for our sample dropbox
  427. }
  428.